package com.robot.mvvm.ui

import android.content.Intent
import android.os.Bundle
import androidx.core.view.get
import androidx.navigation.Navigation
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.NavigationUI
import cn.jpush.android.api.JPushInterface
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.LogUtils
import com.gyf.immersionbar.ktx.immersionBar
import com.robot.mvvm.R
import com.robot.mvvm.databinding.AppActivityMainBinding
import com.robot.mvvm.vm.MainViewModel
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.util.LiveDataBus
import me.goldze.mvvmhabit.BR


@Route(path = AppConstants.Router.Main.A_MAIN)
class MainActivity : GLBaseActivity<AppActivityMainBinding, MainViewModel>() {

    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.app_activity_main

    override fun initVariableId(): Int = BR.viewModel
    override fun initComponents() {
        immersionBar {
            transparentStatusBar()
            keyboardEnable(true)
            fitsSystemWindows(false)
            statusBarDarkFont(false)
        }

        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.fragments) as NavHostFragment
        val navController = navHostFragment.navController;
        NavigationUI.setupWithNavController(binding.bottomNavigationView, navController);
        viewModel.start()
    }


    override fun onSupportNavigateUp(): Boolean {
        return Navigation.findNavController(this, R.id.bottomNavigationView).navigateUp();
    }

    override fun initViewObservable() {
        super.initViewObservable()
        LiveDataBus.get().with(AppConstants.LiveKey.TO_MAIN_TAB, Int::class.java).observe(this) {
            toMainTab(it)
        }
    }
    private fun toMainTab(toMainTab: Int) {
        binding.bottomNavigationView.selectedItemId =
            binding.bottomNavigationView.menu.getItem(toMainTab).itemId
    }


    override fun onDestroy() {

        super.onDestroy()
    }

    override fun onBackPressed() {
//        super.onBackPressed()
        val intent = Intent()
        // 为Intent设置Action、Category属性
        intent.action = Intent.ACTION_MAIN // "android.intent.action.MAIN"
        intent.addCategory(Intent.CATEGORY_HOME) //"android.intent.category.HOME"
        startActivity(intent)
    }
}